package com.org.core.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.org.core.annotations.Table;
import com.org.core.exception.AnnotationException;


public class ClassUtil {
	private static Map<String, String> map = new HashMap<String, String>();
	
	public static Class<?> getClass(String className){
		Class<?> res = null;
		try {
			res = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	
	public static Object newInstance(String clazz) {
		try {
			Object o = Class.forName(clazz).newInstance();
			return o;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String readTableNameByAnnotation(Class<?> clazz){
		String className = clazz.getName();
		if(!map.containsKey(className)) {
			Table table = clazz.getAnnotation(Table.class);
			if(table == null) {
				throw new AnnotationException("请为类"+className+"使用注解Table, 并指定表名");
			}
			String tableName = table.tableName();
			if(StringUtils.isBlank(tableName)) {
				throw new AnnotationException("请为类"+className+"的注解Table指定表名");
			}
			map.put(className, tableName);
		}
		return map.get(className);
	}
	/*
	public static void main(String[] args) {
		Buttel b = new Buttel();
		b.setButtelamount(new BigDecimal("3.00"));
		b.setGroupid(2);
		b.setCreatetime(new Date());
//		Map<Integer, Object> m = createParamsBy(b);
//		System.out.println(m.size());
		
		try {
			System.out.println(invokeMethodToSql(b.getClass()));
		} catch (AnnotationException e) {
			e.printStackTrace();
		}
		
	}*/
}
